#ifndef OMG_DDS_DYNAMIC_DATA_HPP_
#define OMG_DDS_DYNAMIC_DATA_HPP_

/* Copyright (c) 2009-2010, Real-Time Innovations, Inc.
 * Copyright (c) 2010, Object Management Group, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - Neither the names of the above copyright holders nor the names of their
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL A COPYRIGHT HOLDER OR CONTRIBUTOR BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES, LOSS OF USE, DATA, OR PROFITS, OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


#ifdef OMG_DDS_EXTENSIBLE_AND_DYNAMIC_TOPIC_TYPE_SUPPORT


template <typename DELEGATE>
class tdds::type::dynamic::DynamicData : dds::core::Value
{
    OMG_DDS_VALUE_TYPE_NO_DEFAULT(DynamicData)

public:
    class MemberDescriptorIterator :
    public std::iterator<std::forward_iterator_tag,
                         dds::type::dynamic::MemberDescriptor>
    {
    public:
        MemberDescriptorIterator();
        MemberDescriptorIterator(const MemberDescriptorIterator& src);
        ~MemberDescriptorIterator();

    public:
        MemberDescriptorIterator&
        operator=(const MemberDescriptorIterator& src);

        bool operator==(const MemberDescriptorIterator& other) const;
        bool operator!=(const MemberDescriptorIterator& other) const;

        MemberDescriptorIterator& operator++();       // prefix
        MemberDescriptorIterator  operator++(int);    // postfix

        dds::type::dynamic::MemberDescriptor& operator*();

        dds::type::dynamic::MemberDescriptor* operator->();

    private:
        friend class OMG_DDS_DYNAMIC_DATA_MEMBER_DESCRIPTOR_ITERATOR_DELEGATE;
        OMG_DDS_DYNAMIC_DATA_MEMBER_DESCRIPTOR_ITERATOR_DELEGATE impl_;
    };

public:
    class ConstMemberDescriptorIterator :
    public std::iterator<std::forward_iterator_tag,
                         const dds::type::dynamic::MemberDescriptor>
    {
    public:
        ConstMemberDescriptorIterator();
        ConstMemberDescriptorIterator(
            const ConstMemberDescriptorIterator& src);
        ~ConstMemberDescriptorIterator();
        
    public:
        ConstMemberDescriptorIterator&
        operator=(const ConstMemberDescriptorIterator& src);
        
        bool operator==(const ConstMemberDescriptorIterator& other) const;
        bool operator!=(const ConstMemberDescriptorIterator& other) const;
        
        ConstMemberDescriptorIterator& operator++();       // prefix
        ConstMemberDescriptorIterator  operator++(int);    // postfix
        
        const dds::type::dynamic::MemberDescriptor& operator*();
        
        const dds::type::dynamic::MemberDescriptor* operator->();
        
    private:
        friend class OMG_DDS_DYNAMIC_DATA_MEMBER_DESCRIPTOR_ITERATOR_DELEGATE;
        OMG_DDS_DYNAMIC_DATA_MEMBER_DESCRIPTOR_ITERATOR_DELEGATE impl_;
    };
    
public:
    const dds::type::dynamic::DynamicType& type() const;

    MemberDescriptorIterator member_descriptor_begin();
    MemberDescriptorIterator member_descriptor_end();

    ConstMemberDescriptorIterator member_descriptor_begin() const;
    ConstMemberDescriptorIterator member_descriptor_end() const;

    dds::type::MemberId get_member_id_by_name(const std::string& name) const;
    dds::type::MemberId get_member_id_at_index(unsigned long index) const;

    void clear_all_values();
    void clear_nonkey_values();
    void clear_value(dds::type::MemberId id);

    const dds::type::dynamic::DynamicData&
    loan_value(dds::type::MemberId id) const;

    dds::type::dynamic::DynamicData& loan_value(dds::type::MemberId id);
    void return_loaned_value(const dds::type::dynamic::DynamicData& value);

    /**
     * Implementations shall minimally provide specializations for all DDS
     * primitive types, strings, wide strings, and DynamicData.
     */
    template <typename T>
    T get_value(dds::type::MemberId id) const;
    
    /**
     * Implementations shall minimally provide specializations for all DDS
     * primitive types, strings, wide strings, and DynamicData.
     */
    template <typename T>
    void set_value(dds::type::MemberId id, const T& value);

    /**
     * Implementations shall minimally provide specializations of T for all
     * DDS primitive types, strings, wide strings, and DynamicData.
     *
     * T_FWD_ITER is a forward iterator over elements of type T.
     *
     * As an input, element_count indicates the maximum number of elements of
     * the collection to retrieve into the provided iterator. As an output,
     * it indicates the number of elements in the collection, regardless of
     * how many were retrieved. By comparing the input and output values of
     * this argument, applications can detect overflow and underflow
     * conditions.
     */
    template <typename T, typename T_FWD_ITER>
    void get_values(T_FWD_ITER begin,
                    dds::type::MemberId id,
                    uint32_t& element_count =
                        dds::core::LENGTH_UNLIMITED) const;

    /**
     * Implementations shall minimally provide specializations of T for all
     * DDS primitive types, strings, wide strings, and DynamicData.
     *
     * T_RNDA_ITER is a random-access iterator over elements of type T.
     * However, implementations shall not modify the values within the range
     * [begin, end).
     */
    template <typename T, typename T_RNDA_ITER>
    void set_values(dds::type::MemberId id,
                    T_RNDA_ITER begin,
                    T_RNDA_ITER end);

    // --- State: --- //
private:
    friend class OMG_DDS_DYNAMIC_DATA_DELEGATE;
    DELEGATE impl_;
};


#endif  // OMG_DDS_EXTENSIBLE_AND_DYNAMIC_TOPIC_TYPE_SUPPORT


#endif // !defined(OMG_DDS_DYNAMIC_DATA_HPP_)
